<template>
  <div class="header">
    <img class="logo" :src="logo" alt="" />
    <div class="header-menu">
      <el-menu :default-active="routePath" mode="horizontal" :router="true">
        <el-menu-item
          v-for="item in menuData"
          :key="item.path"
          :index="item.path"
        >
          <span>{{ item.title }}</span>
        </el-menu-item>
      </el-menu>
    </div>
    <div class="header-right">
      <div class="header-user-con">
        <!-- 机器人 -->
        <div class="roboat" @click="isRoboat = !isRoboat"></div>
        <!-- 消息中心 -->
        <div class="btn-bell">
          <el-tooltip
            effect="dark"
            :content="message ? `有${message}条未读消息` : `消息中心`"
            placement="bottom"
          >
            <i class="el-icon-lx-notice"></i>
          </el-tooltip>
          <span class="btn-bell-badge" v-if="message"></span>
        </div>
        <!-- 用户头像 -->
        <el-avatar class="user-avator" :size="30" :src="imgurl" />
        <!-- 用户名下拉菜单 -->
        <el-dropdown class="user-name" trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            阿谱up
            <el-icon class="el-icon--right">
              <arrow-down />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="user">个人中心</el-dropdown-item>
              <el-dropdown-item divided command="loginout"
                >退出登录</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
  <!-- 机器人 -->
  <div class="roboat-fixed" v-if="isRoboat">
    <div class="r-head">
      <div class="name">小源</div>
      <div class="reset" @click="isShow = false">
        <RefreshRight class="icon" />
      </div>
      <div class="close" @click="isRoboat = false">
        <Close class="icon" />
      </div>
    </div>
    <el-scrollbar class="r-content">
      <div class="intro" v-if="!isShow">
        <div class="title">Hi 你好，我是Ai助理小源</div>
        <div class="desc">您可以尝试下方的问题，开启体验！</div>
        <div class="tag">
          <div class="item" @click="handleChangeQuestion(0)">
            最近7天为我们带货的前三名主播是谁？带了多少钱？
          </div>
          <div class="item" @click="handleChangeQuestion(1)">
            请为我们公司的新品”酸奶麦香脆枣“写个广告词
          </div>
          <div class="item" @click="handleChangeQuestion(2)">
            最近1个月我们几款产品各自的销售情况
          </div>
        </div>
      </div>
      <div class="answer" v-else>
        <div class="section" v-if="activeIndex === 0">
          <div class="item-box">
            <img src="../assets/img/userIcon.png" class="avatar" alt="" />
            <div class="info high">
              <div class="text">
                最近7天为我们带货的前三名主播是谁？带了多少钱？
              </div>
            </div>
          </div>
          <div class="item-box">
            <img src="../assets/img/answer.png" class="avatar" alt="" />
            <div class="info">
              <el-skeleton :rows="2" v-if="isLoading" animated />
              <template v-else>
                <div class="text">您好带货前三名的主播和销售额如下：</div>
                <img src="../assets/home/a-1.png" class="image" alt="" />
              </template>
            </div>
          </div>
        </div>

        <div class="section" v-if="activeIndex === 1">
          <div class="item-box">
            <img src="../assets/img/userIcon.png" class="avatar" alt="" />
            <div class="info high">
              <div class="text">请为我们公司的新品”酸奶麦香脆枣“写个广告词</div>
            </div>
          </div>
          <div class="item-box">
            <img src="../assets/img/answer.png" class="avatar" alt="" />
            <div class="info">
              <el-skeleton :rows="2" v-if="isLoading" animated />
              <template v-else>
                <div class="text">酸奶加脆枣，一口新鲜，唤醒舌尖的春天</div>
              </template>
            </div>
          </div>
        </div>

        <div class="section" v-if="activeIndex === 2">
          <div class="item-box">
            <img src="../assets/img/userIcon.png" class="avatar" alt="" />
            <div class="info high">
              <div class="text">最近1个月我们几款产品各自的销售情况</div>
            </div>
          </div>
          <div class="item-box">
            <img src="../assets/img/answer.png" class="avatar" alt="" />
            <div class="info">
              <el-skeleton :rows="2" v-if="isLoading" animated />
              <template v-else>
                <div class="text">
                  您好最近一个月各产品总销售额127.5万，其中销售额前5个产品销售额如下：单位（千）
                </div>
                <img src="../assets/home/a-2.png" class="image" alt="" />
              </template>
            </div>
          </div>
        </div>

        <div class="section" v-if="activeIndex === 3">
          <div class="item-box">
            <img src="../assets/img/userIcon.png" class="avatar" alt="" />
            <div class="info high">
              <div class="text">{{ question }}</div>
            </div>
          </div>
          <div class="item-box">
            <img src="../assets/img/answer.png" class="avatar" alt="" />
            <div class="info">
              <el-skeleton :rows="2" v-if="isLoading" animated />
              <template v-else>
                <div
                  class="text"
                  v-for="(item, index) in answerArray"
                  :key="index"
                >
                  {{ item }}
                </div>
              </template>
            </div>
          </div>
        </div>
      </div>
    </el-scrollbar>
    <div class="r-box">
      <textarea
        v-model.trim="textarea"
        class="textarea"
        placeholder="请将您遇到的问题告诉我，使用 Shift + Enter 换行"
        @keydown="handleKeyDown"
      ></textarea>
      <div class="btn-panel">
        <div class="submit" @click="handleSend"></div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted, computed, ref } from "vue";
import { Close, RefreshRight } from "@element-plus/icons-vue";
import { useRoute, useRouter } from "vue-router";
import imgurl from "../assets/img/userIcon.png";
import logo from "../assets/img/logo.png";
import { PostEndpoint } from "../services/gpt.js";
import { ElMessage } from "element-plus";
const username: string | null = localStorage.getItem("ms_username");
const message: number = 2;
const route = useRoute();
const router = useRouter();
const routePath = ref("");
const isRoboat = ref(false);
const isShow = ref(false);

const textarea = ref("");
const question = ref("");
const isLoading = ref(false);
const activeIndex = ref(-1);
const answerArray = ref<any>([]);

function handleChangeQuestion(index: any) {
  activeIndex.value = index;
  isShow.value = true;
  isLoading.value = true;
  setTimeout(() => {
    isLoading.value = false;
  }, 2000);
}

function handleKeyDown(event: any) {
  if (event.keyCode === 13) {
    handleSend();
  }
}

function handleSend() {
  if (textarea.value.length === 0) {
    return ElMessage.error("请输入内容");
  }
  question.value = textarea.value;
  textarea.value = "";
  activeIndex.value = 3;
  isShow.value = true;
  isLoading.value = true;
  PostEndpoint({
    text: question.value,
  })
    .then((res: any) => {
      isLoading.value = false;
      answerArray.value = res.answer.split("\n\n");
    })
    .catch(() => {
      isLoading.value = false;
      answerArray.value = [
        "很抱歉，您的问题我无法回答，我还要多学习学习~",
        "感谢您的理解，谢谢！",
      ];
    });
}

onMounted(() => {
  routePath.value = route.path;
});

// 用户名下拉菜单选择事件
const handleCommand = (command: string) => {
  if (command == "loginout") {
    localStorage.removeItem("ms_username");
    router.push("/login");
  } else if (command == "user") {
    router.push("/user");
  }
};

//#region 菜单栏
const menuData = [
  {
    title: "首页",
    path: "/home",
  },
  {
    title: "市场洞察",
    path: "/market",
  },
  {
    title: "营销策划",
    path: "/devise",
  },
  {
    title: "视觉设计",
    path: "/draw",
  },
  {
    title: "产品创新",
    path: "/product",
  },
  {
    title: "渠道服务",
    path: "/channel",
  },
  {
    title: "知识库",
    path: "/knowledge",
  },
  {
    title: "营销Agent",
    path: "/agent",
  },
];
</script>
<style scoped lang="scss">
.roboat-fixed {
  position: fixed;
  right: 0;
  top: 60px;
  bottom: 0;
  width: 450px;
  background-color: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  z-index: 100;
  .r-head {
    height: 64px;
    border-bottom: solid 1px #e5e7ec;
    display: flex;
    align-items: center;
    padding: 0 20px;
    font-size: 18px;
    font-weight: bold;
    .reset {
      width: 40px;
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-left: 5px;
      cursor: pointer;
      .icon {
        width: 20px;
        height: 20px;
        color: #2171fe;
      }
    }
    .close {
      width: 28px;
      height: 28px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-left: auto;
      cursor: pointer;
      .icon {
        width: 20px;
        height: 20px;
      }
      &:hover {
        background-color: #f5f5f5;
      }
    }
  }
  .r-content {
    height: calc(100vh - 277px);
    .intro {
      padding: 20px;
      .title {
        font-size: 20px;
        color: #2171fe;
        font-weight: bold;
      }
      .desc {
        line-height: 1.6;
        margin-top: 15px;
      }
      .tag {
        overflow: hidden;
        .item {
          margin-top: 15px;
          height: 48px;
          background-color: #f5f5f5;
          border: solid 1px #ddd;
          border-radius: 5px;
          display: flex;
          align-items: center;
          padding: 0 12px;
          line-height: 1.6;
          cursor: pointer;
        }
      }
    }
    .answer {
      overflow: hidden;
      padding: 20px;
      .section {
        overflow: hidden;
        .item-box {
          display: flex;
          margin-bottom: 24px;
          .avatar {
            width: 32px;
            height: 32px;
          }
          .info {
            flex: 1;
            width: 1%;
            margin-left: 12px;
            .text {
              line-height: 32px;
            }
            .image {
              margin-top: 5px;
              width: 100%;
              display: block;
            }
            &.high {
              .text {
                background-color: #f5f5f5;
                border: solid 1px #ddd;
                border-radius: 5px;
                padding: 4px 12px;
              }
            }
          }
        }
      }
    }
  }
  .r-box {
    height: 100px;
    border: solid 1px #e5e7ec;
    margin: 20px;
    border-radius: 4px;
    overflow: hidden;
    padding: 10px 10px 0;
    .textarea {
      font-family: "PingFangSC", "Helvetica Neue", Helvetica, "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
      width: 100%;
      height: 60px;
      border-width: 0;
      outline-style: none;
      outline-width: 0;
      resize: none;
    }
    .btn-panel {
      display: flex;
      justify-content: flex-end;
      .submit {
        width: 40px;
        height: 32px;
        background: url("../assets/img/send.png") no-repeat center;
        background-size: 40px auto;
        cursor: pointer;
      }
    }
  }
}
.header {
  position: relative;
  height: 60px;
  font-size: 22px;
  display: flex;
  align-items: center;
  background-color: #2171fe;
  color: #fff;
  .logo {
    height: 40px;
    margin-left: 20px;
  }
  :deep(.el-menu) {
    background-color: transparent;
    .el-menu-item {
      color: #fff;
      &:hover {
        background-color: rgba(255, 255, 255, 0.3);
        color: #fff;
      }
      &.is-active {
        background-color: #fff;
        border-bottom-width: 0;
      }
    }
  }
}

.collapse-btn {
  width: 250px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  height: 100%;
  float: left;
  padding: 0 21px;
  cursor: pointer;
}

.header-menu {
  flex: 1;
  margin-left: 130px;
  :deep(.el-menu--horizontal) {
    border-bottom-width: 0;
  }
}

.header-right {
  padding-right: 20px;
  align-items: flex-end;
  justify-content: flex-end;
}

.header-user-con {
  display: flex;
  height: 58px;
  align-items: center;
}
.roboat {
  width: 24px;
  height: 24px;
  background: url("../assets/home/logo.png") no-repeat;
  background-size: 24px;
  border-radius: 50%;
  margin-right: 24px;
  cursor: pointer;
}

.btn-fullscreen {
  transform: rotate(45deg);
  margin-right: 5px;
  font-size: 24px;
}

.btn-bell,
.btn-fullscreen {
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  border-radius: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.btn-bell-badge {
  position: absolute;
  right: 4px;
  top: 0px;
  width: 8px;
  height: 8px;
  border-radius: 4px;
  background: #f56c6c;
}

.user-name {
  margin-left: 10px;
}

.user-avator {
  margin-left: 20px;
}

.el-dropdown-link {
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.el-dropdown-menu__item {
  text-align: center;
}
</style>
